package com.kurumi.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;

public class CSVUtils {

	private static final String crlf=System.getProperty("line.separator");
	
	public static boolean csvWriter(OutputStream out,List<String> headers,List<List<String>> records) throws IOException {
		return csvWriter(out,"GBK",headers,records);
	}
	
	public static boolean csvWriter(OutputStream out,String charCode,List<String> headers,List<List<String>> records) throws IOException {
		OutputStreamWriter osw = null;
		CSVPrinter csvPrinter = null;
		CSVFormat csvFormat = CSVFormat.DEFAULT.withRecordSeparator(crlf);
		try {
			osw = new OutputStreamWriter(out, charCode);
			csvPrinter = new CSVPrinter(osw, csvFormat);
			csvPrinter.printRecord(headers);
			for (List<String> record : records) {
				csvPrinter.printRecord(record);
			}
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		} finally {
			try {
				if (csvPrinter != null) {
					csvPrinter.flush();
					csvPrinter.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	
	
	public static boolean csvWriter(String csvFileName,String charCode,List<String> headers,List<List<String>> records) throws IOException {
		OutputStreamWriter osw = null;
		CSVPrinter csvPrinter = null;
		CSVFormat csvFormat = CSVFormat.DEFAULT.withRecordSeparator(crlf);
		try {
			osw = new OutputStreamWriter(new FileOutputStream(csvFileName), charCode);
			csvPrinter = new CSVPrinter(osw, csvFormat);
			csvPrinter.printRecord(headers);
			for (List<String> record : records) {
				csvPrinter.printRecord(record);
			}
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		} finally {
			try {
				if (csvPrinter != null) {
					csvPrinter.flush();
					csvPrinter.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static boolean csvWriter(String csvFileName,List<String> headers,List<List<String>> records) throws IOException {
		return csvWriter(csvFileName,"GBK",headers,records);
	}
	
	
	
	public static List<Map<String,String>> csvReader(String csvFileName,String charCode) throws IOException{
		List<Map<String,String>> records= null;
		Reader reader = null;
		try {
			records = new ArrayList<Map<String,String>>();
			reader = new InputStreamReader(new FileInputStream(csvFileName),"GBK");
			Iterable<CSVRecord> csvRecords = CSVFormat.EXCEL.withHeader().parse(reader);
			for (CSVRecord csvRecord : csvRecords) {
				records.add(csvRecord.toMap());
			}
			return records;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw e;
		}finally{
			try{
				if(reader != null){
					reader.close();
				}
			}catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	}
	
	public static List<Map<String,String>> csvReader(String csvFileName) throws IOException{
		return csvReader(csvFileName,"GBK");
	}
	
	
}
